//
//  TurretObject.m
//  Agility
//
//  Created by Dominique d'Argent on 16.03.11.
//  Copyright 2011 Nicky Nubbel. All rights reserved.
//

#import "TurretObject.h"

// import shot objects
#import "ShotObject.h"
#import "HomingShotObject.h"
#import "HomingMissileShotObject.h"

#import "GameObjectLayer.h"

#define ANGULAR_VELOCITY M_PI_4
#define SHOT_COOLDOWN 2.0

@implementation TurretObject

+ (void)spawnTurretWithIndex:(NSUInteger)index {
    TurretObject *turret = [[self alloc] initWithImageNamed:GAME_TURRET_KEY_BASE];
    turret.position = CGPointMake(240 + 320 * index, 800);
    
    NSString *turretKey = [[GameData sharedGameData] keyForTurretAtIndex:index];
    
    [[GameData sharedGameData] addGameObject:turret forKey:turretKey];
    
    [turret release];
}

- (id)initWithImageNamed:(NSString *)newImageName {
    self = [super initWithImageNamed:newImageName];
    
    if (self) {
        angularVelocity = ANGULAR_VELOCITY;
        
        CGPoint targetPosition = [[GameData sharedGameData] gameObjectForKey:GAME_PLAYER_KEY].position;
        [self updateWithValue:[NSValue valueWithCGPoint:targetPosition]];
        
        [[GameData sharedGameData].gameObjects addObserver:self 
                                    forKeyPath:GAME_PLAYER_KEY
                                       options:NSKeyValueObservingOptionNew 
                                       context:nil];
    }
    
    return self;
}

- (void)dealloc {
    [[GameData sharedGameData].gameObjects removeObserver:self 
                                            forKeyPath:GAME_PLAYER_KEY];
    
    [super dealloc];
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
    if ([keyPath isEqualToString:GAME_PLAYER_KEY]) {
        NSValue *value = [change valueForKey:NSKeyValueChangeNewKey];
        
        if (![value isEqual:[NSNull null]]) {
            CGPoint point = ((GameObject *) value).position;
            
            [self updateWithValue:[NSValue valueWithCGPoint:point]];
        }
    }
}

- (void)updateWithValue:(NSValue *)value {
    angle = [self angleToPoint:[value CGPointValue]];
    durationForRotation = [self durationForRotationToAngle:angle];
    
    [super updateWithValue:value];
}

- (void)updatePositionWithTimeInterval:(NSTimeInterval)timeInterval {
    shotTimeout -= timeInterval;
    if (shotTimeout <= 0 && [self angularDistanceToAngle:angle] <= M_PI_4) {
        shotCount++;
        
        id shotType;
        
        if (shotCount % 20 == 0) {
            shotType = [HomingShotObject class];
        }
        else if(shotCount % 3 == 0) {
            shotType = [HomingMissileShotObject class];
        }
        else {
            shotType = [ShotObject class];
        }
        
        CGFloat currentAngle = self.currentAngle;
        
        CGPoint shotPosition = position;
        shotPosition.x += layer.size.width/2  * sinf(currentAngle);
        shotPosition.y += layer.size.height/2 * cos(currentAngle);
        
        GameObject *target = [[GameData sharedGameData] gameObjectForKey:GAME_PLAYER_KEY];
        [shotType spawnShotAtPosition:shotPosition withTarget:target trajectory:currentAngle];
        
        shotTimeout = SHOT_COOLDOWN;
    }
}

- (void)updateTransformWithTimeInterval:(NSTimeInterval)timeInterval {
}

@end
